//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Validation Application Block
//===============================================================================

using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using EntLibContrib.Validation.Configuration;
using EntLibContrib.Validation.Properties;

namespace EntLibContrib.Validation
{
	/// <summary>
	/// Externally configured Object collection Validator
	/// </summary>
	[ConfigurationElementType(typeof(ExternallyConfiguredObjectCollectionValidatorData))]
	public class ExternallyConfiguredObjectCollectionValidator : ExternallyConfiguredObjectValidator
	{
		#region Construction
		/// <summary>
		/// Initializes a new instance of the <see cref="ExternallyConfiguredObjectCollectionValidator"/> class.
		/// </summary>
		/// <param name="ruleSet"></param>
		/// <param name="configurationFilePath"></param>
		public ExternallyConfiguredObjectCollectionValidator(string ruleSet, string configurationFilePath)
			: base(ruleSet, configurationFilePath)
		{
		}
		#endregion

		#region Protected Methods
		/// <summary>
		/// Does the validate.
		/// </summary>
		/// <param name="objectToValidate">The object to validate.</param>
		/// <param name="currentTarget">The current target.</param>
		/// <param name="key">The key.</param>
		/// <param name="validationResults">The validation results.</param>
		public override void DoValidate(object objectToValidate, object currentTarget, string key, ValidationResults validationResults)
		{
			if (objectToValidate != null)
			{
				IEnumerable enumerable = objectToValidate as IEnumerable;
				if (enumerable != null)
				{
					foreach (object element in enumerable)
					{
						base.DoValidate(element, element, null, validationResults);
					}
				}
				else
				{
					this.LogValidationResult(validationResults, Resources.ValidationErrorIncompatibleType, currentTarget, key);
				}
			}
		}
		#endregion
	}
}
